<?php
	$rootFolder = $_SERVER['DOCUMENT_ROOT'];
    require $rootFolder."/YumeAds/Classes/ChannelService.php";
	require $rootFolder."/YumeAds/Classes/DatabaseContext.php";

    $videoId = $_GET["videoId"];
	$chanelService = new ChannelService();
	$video = $chanelService->getVideoById($videoId);
	$channel = $chanelService->getChannelByVideoId($videoId);
?>
<!DOCTYPE HTML>
<html>
	<head>
		<meta name="viewport" content="width=device-width">
		<script src="js/yumeLib/jquery-1.6.2.min.js" type="text/javascript"></script>
		<script src="js/yumeLib/yume_html5_sdk.js" type="text/javascript"></script>
		<script src="js/yumeLib/yume_vast_util.js" type="text/javascript"></script>
		<script src="js/yumeLib/detectmobilebrowser.js" type="text/javascript"></script>
	</head>
	<body>
		<div id="html5_video_div" style="border: 1px solid red; margin-top: 100px; width: 480px; height: 390px; float: left; position: absolute;">
			<a href="javascript:void(0)" id="hRefClickTag" style="cursor: default;">
				<video id="html5_video" poster="" width="480" height="360" controls>
					<source src="<?php echo $video["Link"]?>" type="video/mp4">
					<i>OOPS, Your Browser doesn't support the HTML5 &lt;video&gt; element.</i> 
				</video>
				<video id="Video_player" poster="" width="480" height="360" controls>
					<source src="<?php echo $video["Link"]?>" type="video/mp4">
					<i>OOPS, Your Browser doesn't support the HTML5 &lt;video&gt; element.</i> 
				</video>
			</a> 
		</div>
		<script>
			function getAdDomainUrl(url){
				var result = "http://";
				var nonDomainPart = url.substr(result.length,url.length);
				var domainPart = nonDomainPart.split("/")[0];
				return result+domainPart+"/";
			}
			
			function getDomainId(url){
				return url.substr(url.indexOf("domain=")+"domain=".length,url.length);
			}
			
			function getPrerollPlaylist(url){
				var result = "http://";
				var nonDomainPart = url.substr(result.length,url.length);
				var playListPart = nonDomainPart.split("/")[1]; 
				return playListPart.split("?")[0];
			}
		
			$('#Video_player').hide();
			var yumeSDKInstance = new YuMeHTML5SDK();
			var yumeInitObj = new YuMeHTML5SDKInitObject(); 
			//http://shadow01.yumenetworks.com/dynamic_preroll_playlist.vast2xml?domain=1552hCkaKYjg
			yumeInitObj.cbMedRectDivId = "cb_medrect1_div";
			//this value needs to be the same as the MedRect div's id
			
			yumeInitObj.cbMedRectIFrameId = "cb_medrect1_frame";
			//this value needs to be the same as the MedRect frame's id
			
			var url = '<?php echo $channel["VAST"]?>';
			console.log(url);
			yumeInitObj.adDomainUrl = getAdDomainUrl(url);
			console.log(yumeInitObj.adDomainUrl);
			// NOTE: For testing this can be set to "http://shadow01.yumenetworks.com/"
			
			yumeInitObj.domainId = getDomainId(url);
			console.log(yumeInitObj.domainId);
			//yumeInitObj.domainId = "211DaVuJgGj";
			// NOTE: For testing this should be set to "211DaVuJgGj"
			
			yumeInitObj.prerollPlaylist = getPrerollPlaylist(url);
			console.log(yumeInitObj.prerollPlaylist);
			
			yumeInitObj.html5VideoId = "html5_video";
			//this value needs to be the same as the HTML5 video element's id on the page
			yumeInitObj.html5VideoDivId = "html5_video_div"; //this value needs to be the same as the HTML5 video div element's id on the page
			yumeInitObj.clickTagHRefId = "hRefClickTag";
			//this value needs to be the same as the anchor tag's id around the <video> element
			
			yumeSDKInstance.yume_init(yumeInitObj);
			
			yumeSDKInstance.yume_startAd(YuMeHTML5SDK.prototype.yume_adBlockType.PREROLL_BLOCK);
			function yumeSDKAdEventListener(yume_event, yume_eventInfo) {
			switch(yume_event) {
			case YuMeHTML5SDK.prototype.yume_adEvent.AD_PRESENT:
			//alert("Received AD_PRESENT event from YuMe HTML5 Plug-in...");
			//do something
			break;
			case YuMeHTML5SDK.prototype.yume_adEvent.AD_ABSENT:
			alert("Received AD_ABSENT event from YuMe HTML5 Plug-in...");
			//do something
			break;
			case YuMeHTML5SDK.prototype.yume_adEvent.AD_PLAYING:
			//alert("Received AD_PLAYING event from YuMe HTML5 Plug-in...");
			//do something
			break;
			case YuMeHTML5SDK.prototype.yume_adEvent.AD_COMPLETED:
			
			$('#html5_video').hide();
			$('#Video_player').show();
			$('#Video_player')[0].play();
			//alert("Received AD_COMPLETED event from YuMe HTML5 Plug-in...");
			//do something
			break;
			case YuMeHTML5SDK.prototype.yume_adEvent.AD_ERROR:
			$('#Video_player').show();
			alert("Received AD_ERROR event from0 YuMe HTML5 Plug-in, Error Info:" + yume_eventInfo);
			//do something
			break;
				case YuMeHTML5SDK.prototype.yume_adEvent.AD_CLICKED2SITE:
					$('#html5_video').hide();
					$('#Video_player').show();
					$('#Video_player')[0].play();
			//alert("Received AD_CLICKED2SITE event from YuMe HTML5 Plug-in...");
			//do something
			break;
			   case YuMeHTML5SDK.prototype.yume_adEvent.AD_CLICKED2vIDEO:
			   $('#html5_video').hide();
					$('#Video_player').show();
					$('#Video_player')[0].play();
			//alert("Received AD_CLICKED2VIDEO event from YuMe HTML5 Plug-in...");
			//do something
			break;
			
			   case YuMeHTML5SDK.prototype.yume_adEvent.PIP_VIDEO_PLAYING:
			//alert("Received PIP_VIDEO_PLAYING event from YuMe HTML5 Plug-in...");
			//do something
			break;
			case YuMeHTML5SDK.prototype.yume_adEvent.AD_CB_IFRAME:
			//alert("Received AD_CB_IFRAME event from YuMe HTML5 Plug-in..." + yume_eventInfo);
			//do something
			break;
			default:
			break;
			}
			}
			
			//for fetching Preroll Ads
			
			
		</script>
	</body>
</html>